package com.jcfk.eam.service.main;

import com.jcfk.eam.domain.po.main.EamRepairOrderPart;
import com.jcfk.eam.domain.query.main.EamRepairOrderPartQuery;
import com.jcfk.eam.domain.dto.main.EamRepairOrderPartDTO;
import com.jcfk.eam.dao.main.EamRepairOrderPartDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;

import java.util.List;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 计划维修备件清单
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-09-06
 */
@Service
public class EamRepairOrderPartService extends BaseService<EamRepairOrderPartDao,EamRepairOrderPart> {

    /**
     * EamRepairOrderPart分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamRepairOrderPartDTO> queryByPage(DataPage<EamRepairOrderPartDTO> page, EamRepairOrderPartQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


        /**
     * 判断EamRepairOrderPart是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamRepairOrderPartDTO dto) {
        QueryWrapper<EamRepairOrderPart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
                                                        
        return RetResult.ok();
    }

    /**
     * 插入计划维修备件清单
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamRepairOrderPartDTO dto) {

        // 保存
        EamRepairOrderPart item = new EamRepairOrderPart();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新计划维修备件清单
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamRepairOrderPartDTO dto) {

        // 保存
        EamRepairOrderPart item = new EamRepairOrderPart();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除计划维修备件清单(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamRepairOrderPartDTO dto) {

        EamRepairOrderPart item = new EamRepairOrderPart();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamRepairOrderPart> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamRepairOrderPart item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
                item.setDeleted(0);
                                                                                                                            item.setCreatedUserName(SecurityUtils.getUserName());
                                                                                }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

                                                        item.setLastUpdatedUserName(SecurityUtils.getUserName());
            
    }

    /**
     * 根据主键获取计划维修备件清单信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamRepairOrderPartDTO get(String id) {
        // 查询数据
        EamRepairOrderPart item = this.baseMapper.selectById(id);
            EamRepairOrderPartDTO dto = new EamRepairOrderPartDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }

    

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamRepairOrderPartDTO>
     */
    public List<EamRepairOrderPartDTO> getList(EamRepairOrderPartQuery query) {
        // 查询条件
        QueryWrapper<EamRepairOrderPart> qw = new QueryWrapper<>();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                // 查询数据
        List<EamRepairOrderPart> list = super.list(qw);

        return convertList(list, EamRepairOrderPartDTO.class);
    }

    /**
     * 根据主键获取计划维修备件清单信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
            EamRepairOrderPart item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
        }

    /**
     * 根据维修单获取维修备件和备件的详细信息
     * @param repairId 维修单id
     * @return 维修备件和备件的详细信息
     */
    public List<EamRepairOrderPartDTO> queryPartListByRepair(String repairId) {
        return this.baseMapper.queryPartListByRepair(repairId);
    }
}

